package com.coollang.smartbra.fragment;

import android.content.Context;
import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.SportStatisticsActivity1;
import com.coollang.smartbra.adapter.DaySportCharAdapter;
import com.coollang.smartbra.adapter.WeekSportCharAdapter;
import com.coollang.smartbra.base.BaseFragment;
import com.coollang.smartbra.interfaces.ActionType;
import com.coollang.smartbra.model.SportRecordModel;
import com.coollang.smartbra.model.WeekActionModel;
import com.coollang.smartbra.presenter.SportStatisticsFragmentPresenter;
import com.coollang.smartbra.utils.DisplayUtils;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.WindowManagerUtils;
import com.coollang.smartbra.views.HorizontalListView;

import java.util.List;

import static com.coollang.smartbra.utils.TimeUtils.secToTime;

/**
 * @author: Timmy
 * @data: 2018/1/30
 * @Fuction:
 */

public class SportStatisticsFragment extends BaseFragment {

    private View mView;
    private Context mContext;
    private static int mPosition;
    private HorizontalListView mDayListView;
    private HorizontalListView mWeekListView;

    RadioGroup mDayRadio;
    RadioButton mRbMale;
    RadioButton mRbFemale;

    TextView mTvItem0_0;
    TextView mTvItem0_1;
    TextView mTvItem1_0;
    TextView mTvItem1_1;
    TextView mTvItem2_0;
    TextView mTvItem2_1;
    TextView mTvItem3_0;
    TextView mTvItem3_1;
    private DaySportCharAdapter mDaySportCharAdapter;
    private WeekSportCharAdapter mWeekSportCharAdapter;
    private List<SportRecordModel.DataBean> mDataBeanList;
    private SportStatisticsFragmentPresenter mFragmentPresenter;


    public static SportStatisticsFragment newInstance(int position) {
        mPosition = position;
        return new SportStatisticsFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    protected View inflateView(LayoutInflater inflater) {
        mView = inflater.inflate(R.layout.fragment_sport_statistics, null, false);
        return mView;
    }

    @Override
    protected void initView(View view) {
        mDayListView = view.findViewById(R.id.day_horizontal_listView);
        mWeekListView = view.findViewById(R.id.week_horizontal_listView);

        mDayRadio = view.findViewById(R.id.sex_radio);
        mDayRadio.setOnCheckedChangeListener(mOnCheckedChangeListener);

        mRbMale = view.findViewById(R.id.rb_male);
        mRbFemale = view.findViewById(R.id.rb_female);

        mTvItem0_0 = view.findViewById(R.id.tv_item0_0);
        mTvItem0_1 = view.findViewById(R.id.tv_item0_1);
        mTvItem1_0 = view.findViewById(R.id.tv_item1_0);
        mTvItem1_1 = view.findViewById(R.id.tv_item1_1);
        mTvItem2_0 = view.findViewById(R.id.tv_item2_0);
        mTvItem2_1 = view.findViewById(R.id.tv_item2_1);
        mTvItem3_0 = view.findViewById(R.id.tv_item3_0);
        mTvItem3_1 = view.findViewById(R.id.tv_item3_1);
        setDayData();
        initListView();
    }


    @Override
    protected void initData() {
        mFragmentPresenter = new SportStatisticsFragmentPresenter(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void setHistoryData(List<SportRecordModel.DataBean> data) {
        // 收到数据了
        mDataBeanList = data;
        mFragmentPresenter.setData(mDataBeanList);
        mDaySportCharAdapter.notifWeekOrDay(data);
        int i = DisplayUtils.dip2px(mContext, 30) * (data.size() - 1) + DisplayUtils.dip2px(mContext, 16) * (data.size() - 2);
        mDayListView.scrollTo(i);
        setDayBottomUI(data.get(mDataBeanList.size() - 1));
    }

    /**
     * 初始化 ListView
     */
    private void initListView() {
        mDaySportCharAdapter = new DaySportCharAdapter(mContext, getResources().getDisplayMetrics().heightPixels - WindowManagerUtils.getStatusBarHeight(getActivity()));
        mDayListView.setAdapter(mDaySportCharAdapter);
        mDayListView.setOnItemClickListener(mDayOnItemClickListener);

        mWeekSportCharAdapter = new WeekSportCharAdapter(mContext, getResources().getDisplayMetrics().heightPixels - WindowManagerUtils.getStatusBarHeight(getActivity()));
        mWeekListView.setAdapter(mWeekSportCharAdapter);
        mWeekListView.setOnItemClickListener(mWeekOnItemClickListener);
    }

    /**
     * 设置天UI
     */
    private void setDayData() {
        // 显示天条目, 隐藏周条目
        mDayListView.setVisibility(View.VISIBLE);
        mWeekListView.setVisibility(View.GONE);

        mRbMale.setBackgroundResource(R.drawable.shape_yellow_5corners_left);
        mRbFemale.setBackgroundResource(R.color.tra);

        if (mDataBeanList != null && mDataBeanList.size() > 0) {
            mDaySportCharAdapter.notifOnItemClick(mDataBeanList.size() - 1);
            setDayBottomUI(mDataBeanList.get(mDataBeanList.size() - 1));
        }
    }

    /**
     * 设置周UI
     */
    private void setWeekData() {
        // 显示周条目 隐藏天条目
        mDayListView.setVisibility(View.GONE);
        mWeekListView.setVisibility(View.VISIBLE);

        mRbMale.setBackgroundResource(R.color.tra);
        mRbFemale.setBackgroundResource(R.drawable.shape_yellow_5corners_right);

        // 计算数据
        List<WeekActionModel> data = mFragmentPresenter.getWeekData(mDataBeanList);

        if (data != null && data.size() > 0) {
            mWeekSportCharAdapter.notifWeekOrDay(data);
            mWeekSportCharAdapter.notifOnItemClick(data.size() - 1);
            setWeekBottomUI(data.get(data.size() - 1));
        }

        int i = DisplayUtils.dip2px(mContext, 30) * (data.size() - 1) + DisplayUtils.dip2px(mContext, 16) * (data.size() - 2);
        mWeekListView.scrollTo(i);
    }

    private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId) {
                case R.id.rb_male:
                    setDayData();
                    break;
                case R.id.rb_female:
                    setWeekData();
                    break;
                default:
                    break;
            }
        }
    };

    private AdapterView.OnItemClickListener mDayOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mDaySportCharAdapter.notifOnItemClick(position);
            setDayBottomUI((SportRecordModel.DataBean) mDaySportCharAdapter.getItem(position));
        }
    };

    /**
     * 设置天底部数据
     *
     * @param bean
     */
    private void setDayBottomUI(SportRecordModel.DataBean bean) {
        String tvCount = "";
        int count = bean.getCount();

        int actionTypeCode = ((SportStatisticsActivity1) mContext).getActionTypeCode();

        if (actionTypeCode == ActionType.PBZC) {
            // 平板支撑
            tvCount = secToTime(count);
            mTvItem0_1.setText("支撑时长");

        } else {
            tvCount = count + "";
            mTvItem0_1.setText("个数");
        }
        mTvItem0_0.setText(tvCount);

        if (count != 0) {
            int fault = bean.getFault();
            int sum = fault + count;
            int current = (int) (((float) count / sum) * 100);
            mTvItem1_0.setText(String.valueOf(current));
        } else {
            mTvItem1_0.setText(String.valueOf(0));
        }
        mTvItem1_1.setText("正确率: %");

        mTvItem2_0.setText(secToTime(bean.getDuration()));
        mTvItem2_1.setText("活跃: 分钟");

        mTvItem3_0.setText(String.valueOf(bean.getHeartRate()));
        mTvItem3_1.setText("心率: 次/分钟");

    }

    private AdapterView.OnItemClickListener mWeekOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mWeekSportCharAdapter.notifOnItemClick(position);
            setWeekBottomUI((WeekActionModel) mWeekSportCharAdapter.getItem(position));
        }
    };

    /**
     * 设置周底部数据
     *
     * @param model
     */
    private void setWeekBottomUI(WeekActionModel model) {
        int currDay = TimeUtils.differentDaysByMillisecond(model.getFirstTimeStamp(), model.getLastTimeStamp());

        String tvCount;
        int actionTypeCode = ((SportStatisticsActivity1) mContext).getActionTypeCode();
        if (actionTypeCode == ActionType.PBZC) {
            // 平板支撑
            tvCount = TimeUtils.secToTime(model.getCount() / currDay);
            mTvItem0_1.setText("日均支撑时长");
        } else {
            tvCount = model.getCount() / currDay + "";
            mTvItem0_1.setText("日均个数");
        }
        mTvItem0_0.setText(tvCount);

        mTvItem1_0.setText(String.valueOf(currDay));
        mTvItem1_1.setText("运动天数");

        mTvItem2_0.setText(secToTime((int) (model.getDuration() / currDay)));
        mTvItem2_1.setText("日均活跃: 分钟");

        mTvItem3_0.setText(String.valueOf(model.getHeartRate() / currDay));
        mTvItem3_1.setText("平均心率: 次/分钟");
    }
}
